<template>
	<el-card>
		<el-space>
			<el-space>
				<el-button type="primary" icon="Plus" @click="addOrUpdateHandle(false, null)">新增</el-button>
			</el-space>
			<el-space>
				<el-button plain @click="toggleExpandAll()">
					<template v-if="!isExpandAll">
						全部展开&nbsp;<el-icon><ArrowDown /></el-icon>
					</template>
					<template v-else>
						全部收起&nbsp;<el-icon><ArrowUp /></el-icon>
					</template>
				</el-button>
			</el-space>
		</el-space>
		<el-table
			v-if="refreshTable"
			v-loading="state.dataListLoading"
			:default-expand-all="isExpandAll"
			:data="state.dataList"
			row-key="id"
			border
			class="layout-table"
		>
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
			<!-- <el-table-column prop="id" label="id" header-align="center" align="center"></el-table-column>
			<el-table-column prop="pid" label="上级ID" header-align="center" align="center"></el-table-column> -->
			<el-table-column prop="templateName" label="模板名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="parentName" label="上级模板名称" header-align="center" align="center"></el-table-column>
			<!-- <el-table-column prop="templateImg" label="模板截图" header-align="center" align="center"></el-table-column> -->
			<el-table-column prop="templateImg" label="模板截图" header-align="center" align="center" width="100px">
				<template #default="scope">
					<el-image style="width: 100%; height: 100%" :src="PUBLIC_URL + scope.row.templateImg" />
				</template>
			</el-table-column>
			<!-- <el-table-column prop="readmeFile" label="模板描述MD" header-align="center" align="center"></el-table-column> -->
			<!-- <el-table-column prop="templateZip" label="模板压缩包文件" header-align="center" align="center"></el-table-column>
			<el-table-column prop="templatePath" label="模板压缩包路径" header-align="center" align="center"></el-table-column> -->
			<el-table-column prop="createTime" label="创建时间" header-align="center" align="center" width="250"></el-table-column>
			<el-table-column prop="version" label="版本号" header-align="center" align="center" width="80"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="300">
				<template #default="scope">
					<el-button type="primary" link @click="addOrUpdateHandle(false, scope.row)">新增</el-button>
					<el-button type="primary" link @click="addOrUpdateHandle(true, scope.row)">修改</el-button>
					<!-- <el-button type="primary" link @click="updateAttachmentHandle(scope.row)">更新附件</el-button> -->
					<el-button type="primary" link @click="downloadAttachPkgHandle(scope.row)">下载模板</el-button>
					<el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update>
		<update-attachment ref="updateAttachmentRef" @refresh-data-list="getDataList"></update-attachment>
	</el-card>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks'
import { nextTick, reactive, ref } from 'vue'
import AddOrUpdate from './add-or-update.vue'
// import UpdateAttachment from './update-attachment.vue'
import { IHooksOptions } from '@/hooks/interface'
import { ArrowDown, ArrowUp } from '@element-plus/icons-vue'
import { min } from 'xe-utils'

import { useDownloadPkgApi } from '@/api/genTemplateTree'

const PUBLIC_URL = import.meta.env.VITE_API_URL
const state: IHooksOptions = reactive({
	dataListUrl: '/gen/gen_template_tree/list',
	deleteUrl: '/gen/gen_template_tree',
	isPage: false
})

const addOrUpdateRef = ref()
const addOrUpdateHandle = (isUpdate: Boolean, row: any) => {
	addOrUpdateRef.value.init(isUpdate, row)
}

// const updateAttachmentRef = ref()
// const updateAttachmentHandle = (row: any) => {
// 	updateAttachmentRef.value.init(row)
// }

const { getDataList, deleteBatchHandle } = useCrud(state)

// 是否展开，默认全部折叠
const isExpandAll = ref(false)
// 是否重新渲染表格状态
const refreshTable = ref(true)

// 切换 展开和折叠
const toggleExpandAll = () => {
	refreshTable.value = false
	isExpandAll.value = !isExpandAll.value
	nextTick(() => {
		refreshTable.value = true
	})
}

const downloadAttachPkgHandle = (row: any) => {
	useDownloadPkgApi(row.id)
}
</script>
